package com.srz.dpm.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srz.dpm.core.dto.ExcelDictDTO;
import com.srz.dpm.core.listener.ExcelDictDTOListener;
import com.srz.dpm.core.mapper.DictMapper;
import com.srz.dpm.core.mapper.ProductParametersMapper;
import com.srz.dpm.core.pojo.entity.Dict;
import com.srz.dpm.core.pojo.entity.ProductParameters;
import com.srz.dpm.core.service.DictSercive;
import com.srz.dpm.core.service.ProductParametersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品基本信息表 服务实现类
 * </p>
 *
 * @author srz
 * @since 2021-09-09
 */
@Slf4j
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictSercive {


    @Transactional(rollbackFor = {Exception.class})//事务
    @Override
    public void importDatao(InputStream inputStream) {

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, ExcelDictDTO.class, new ExcelDictDTOListener(baseMapper)).sheet().doRead();
        log.info("importData finished");
        log.info("导入成功");

    }


    @Override
    public List<ExcelDictDTO> listDictData() {

        List<Dict> dictList = baseMapper.selectList(null);
        //创建ExcelDictDTO列表，将Dict列表转换成ExcelDictDTO列表
        ArrayList<ExcelDictDTO> excelDictDTOList = new ArrayList<>(dictList.size());
        dictList.forEach(dict -> {

            ExcelDictDTO excelDictDTO = new ExcelDictDTO();
            BeanUtils.copyProperties(dict, excelDictDTO);
            excelDictDTOList.add(excelDictDTO);
        });
        return excelDictDTOList;
    }
}
